package com.bilgeadam.conversationmanagement.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bilgeadam.conversationmanagement.model.Product;

/**
 *
 * @author Bora SAYINER
 * @since Dec 28, 2014 7:01:13 PM
 * @version 1.0
 */
public class ShoppingCartServlet extends HttpServlet {

	private final List<Product> products;

	public ShoppingCartServlet() {
		this.products = new ArrayList<Product>();
	}

	@Override
	public void init() throws ServletException {
		products.add(new Product(1, "IPhone 6", "Yeni çıkan iphone versiyonu.", 2500f));
		products.add(new Product(2, "Samsung S5", "10 numara telefon. S6 çıkacak", 2300f));
		products.add(new Product(3, "Doğan SLX", "Yılların eskitemediği araba. Tüplü ve Öfkeli", 5500f));
		products.add(new Product(4, "Ekmek", "150 gram halk ekmek", 1f));
		products.add(new Product(5, "Kaban", "Hakan marka kaban. A kalite kaban.", 1200f));
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uri = req.getRequestURI();
		if (uri.endsWith("showProducts")) {
			printShowProductPages(resp);
		}
	}

	private void printShowProductPages(HttpServletResponse resp) throws IOException {
		resp.setContentType("text/html");
		resp.setCharacterEncoding("utf-8");
		StringBuffer stringBuffer = new StringBuffer();
		stringBuffer.append("<!DOCTYPE html>");
		stringBuffer.append("<html>");
		{
			stringBuffer.append("<head>");
			{
				stringBuffer.append("<meta charset='utf-8'>");
				stringBuffer.append("<title>Products</title>");
			}
			stringBuffer.append("</head>");
			stringBuffer.append("<body>");
			{
				stringBuffer.append("<ul>");
				{
					for (Product product : products) {
						stringBuffer.append("<li>");
						{
							stringBuffer.append(product.getName());
						}
						stringBuffer.append("</li>");
					}
				}
				stringBuffer.append("</ul>");
			}
			stringBuffer.append("</body>");
		}
		stringBuffer.append("</html>");
		resp.getWriter().print(stringBuffer.toString());
	}
}
